<?php

namespace App\Http\Controllers\Admin;
use App\Http\Model\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;

require_once '../resources/org/code/Captcha.php';

class LoginController extends CommonController{
    public function login(Request $request){
        $input = Input::all();
        //  获取数据库中的user信息
        $user=User::first();
        if($input){
            //  获取存储在session中的验证码
            $_captcha=$request->session()->get('captcha');
           // 验证码校验
            if(strtoupper($input['code'])!=strtoupper($_captcha)){
                return back()->with('msg','验证码错误');
            }
            //用户名和密码校验
            if($user->user_name != $input['user_name'] || Crypt::decrypt($user->user_pass)!= $input['user_pass']){
                return back()->with('msg','用户名或者密码错误！');
            }
            session(['user'=>$user]);
            return redirect('admin/index');
        }else{
            return view('admin.login');
        }
    }

    public function code()
    {
        $captcha = new \Captcha();
        $captcha->showImg();
//        将验证码存在seeeion中
        session(['captcha' => $captcha->getCaptcha()]);
    }

    public function quit(){
        session(['user'=>null]);
        return redirect('admin/login');
    }
}
